using System;
using System.Collections;
using BMC.ARSystem;

namespace WMExample
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			Server s = new Server();
			s.Login("localhost", "Demo", "", "");
			
			string formName = "WMExample";
			ArrayList entries = s.GetListEntry(formName, "", 0, 0);
			foreach (EntryDescription ed in entries)
			{
				Console.WriteLine("Entry Id ({0}); Description ({1})", ed.EntryId.ToString(), ed.Description);
				FieldValueList fvl = s.GetEntry(formName, ed.EntryId);
				foreach (uint f in fvl.Fields)
				{
					System.Diagnostics.Debug.WriteLine(String.Format("Field Id ({0}); Value ({1})", f.ToString(), fvl[f]));
					if (f == 15) // our status history field
					{
						string encoded = (string) fvl[f];
						//It would be nice to give a status history decoding function in the API, 
						// but for now its just a string ...
						Console.WriteLine("Status history ({0}", encoded);

						//Before .NET API implements this function, lets do the decode ourselves for now (workaround)...
						StatusHistory sh = StatusHistory.Decode(encoded);
						foreach (StatusHistoryItem shi in sh)
						{
							Console.WriteLine("StatusHistory Time ({0}); User ({1})", shi.ModifiedTime, shi.ModifiedBy);
						}

					}
					else if (f == 536870913) // our diary field
					{
						BMC.ARSystem.DiaryList dl = (BMC.ARSystem.DiaryList) fvl[f];
						foreach (BMC.ARSystem.DiaryEntry de in dl)
						{
							Console.WriteLine("Diary Entry Time ({0}); User ({1}); Text ({2})", de.Time, de.User, de.Text);
						}
					}
				}
			}

		}
	}
	public class StatusHistoryItem
	{
		public StatusHistoryItem(System.DateTime modifiedTime, string modifiedBy)
		{
			m_modifiedTime = modifiedTime;
			m_modifiedBy = modifiedBy;
		}

		public System.DateTime ModifiedTime
		{
			get
			{
				return m_modifiedTime;
			}

		}

		public string ModifiedBy
		{
			get
			{
				return m_modifiedBy;
			}
		}

		private System.DateTime m_modifiedTime;
		private string			m_modifiedBy;
	}

	public class StatusHistory : CollectionBase
	{
	
		public int Add(StatusHistoryItem value)
		{
			return this.List.Add(value);
		}
	
		public StatusHistoryItem this[int index]
		{
			get
			{
				return (StatusHistoryItem) this.List[index];
			}
			set
			{
				this.List[index] = value; 
			}
		}

		public static StatusHistory Decode(string encoded)
		{
			StatusHistory statusHistory = new StatusHistory();
			string[] lines = encoded.Split(new char [] { '\x0003' });
			foreach (string line in lines)
			{
				string[] parts = line.Split(new char [] { '\x0004' });
				if (parts.Length == 2) 
				{
					System.DateTime date = new DateTime(1970, 1, 1, 0, 0, 0);
					date = date.AddSeconds(System.Convert.ToUInt32(parts[0]));
					string user = parts[1];
					StatusHistoryItem entry = new StatusHistoryItem(date, user);
					statusHistory.Add(entry);
				}
				else
					statusHistory.Add(new StatusHistoryItem(System.DateTime.MaxValue , "")); // Dummy placeholder since this does not have value yet
			}
			return statusHistory;
		}
	}
}
